package com.fenghuajueli.module_user.activity.bindphone;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils;
import com.fenghuajueli.libbasecoreui.utils.QuickClickUtils;
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.activity.UserViewModelBaseActivity;
import com.fenghuajueli.module_user.databinding.ActivityBindPhoneBinding;
import com.fenghuajueli.module_user.model.UserViewModel;
import com.fenghuajueli.module_user.network.result.TemplateResult;


/**
 * 付款成功之后验证手机号。只有是用户用手机号登录的才要验证手机号是否是他的
 */
@Route(path = UserModuleRoute.USER_BIND_PHONE_ACTIVITY)
public class BindPhoneActivity extends UserViewModelBaseActivity implements View.OnClickListener {

    private boolean pwdIsShow = false;
    ActivityBindPhoneBinding binding;

    private final UserViewModel userViewModel = new UserViewModel();

    public static final int TYPE_PHONE = 0;
    public static final int TYPE_THIRD = 1;

    // 0表示手机号登录购买vip之后验证手机号使用   1表示微信登录购买vip之后绑定手机使用
    @Autowired(name = "type")
    public int actionType = -1;


    @Override
    public BaseViewModel setBaseViewModel() {
        return userViewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBindPhoneBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        if (actionType == -1) {
            ToastUtils.showShort("参数错误！");
            finish();
            return;
        }
        if (!UserInfoUtils.getInstance().isLogin()) {
            ToastUtils.showShort("当前状态未登录");
            finish();
            return;
        }

        binding.btnComplete.setOnClickListener(this);
        binding.btnSendCode.setOnClickListener(this);
        binding.ivClose.setOnClickListener(this);
        binding.ivChangePwdStatus.setOnClickListener(this);
        userViewModel.verificationCodeTime.observe(this, time -> {
            binding.btnSendCode.setText(time + "s");
            binding.btnSendCode.setEnabled(false);
            if (time == 0) {
                binding.btnSendCode.setEnabled(true);
                binding.btnSendCode.setText("获取验证码");
            }
        });
        if (actionType == TYPE_PHONE) {
            binding.tvTitle.setText("激活手机号");
            binding.etInputPassword.setVisibility(View.INVISIBLE);
            if (UserInfoUtils.getInstance().isPhoneNum()) {
                String phone = UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getPhone();
                if (!TextUtils.isEmpty(phone)) {
                    binding.etInputPhone.setText(phone);
                }
            }
        } else if (actionType == TYPE_THIRD) {
            binding.tvTitle.setText("绑定手机号");
            binding.etInputPassword.setVisibility(View.VISIBLE);
        }
        userViewModel.templateResult.observe(this, result -> {
            if (result instanceof TemplateResult.Success){
                String themeColors = ((TemplateResult.Success) result).getData().getTheme_color().getTheme_colors();
                binding.btnComplete.getBackground().setTint(Color.parseColor(themeColors));
                binding.btnSendCode.getBackground().setTint(Color.parseColor(themeColors));
            }
        });
    }


    @Override
    public void onClick(View view) {
        if (QuickClickUtils.isFastClick()) {
            return;
        }
        int viewId = view.getId();
        if (viewId == R.id.btnComplete) {
            if (userViewModel != null) {
                if (actionType == TYPE_PHONE) {
                    userViewModel.activePhone(this, UserInfoUtils.getInstance().getUserInfoEntity().getToken()
                            , binding.etInputPhone.getText().toString().trim(),
                            binding.etInputCode.getText().toString().trim());
                } else if (actionType == TYPE_THIRD) {
                    userViewModel.bindPhone(this, UserInfoUtils.getInstance().getUserInfoEntity().getToken()
                            , binding.etInputPhone.getText().toString().trim(),
                            binding.etInputCode.getText().toString().trim(),
                            binding.etInputPassword.getText().toString().trim());
                }
            }
        } else if (viewId == R.id.ivClose) {
            finish();
        } else if (viewId == R.id.btnSendCode) {
            if (userViewModel != null) {
                userViewModel.getVerificationCode(this, binding.etInputPhone.getText().toString().trim());
            }
        } else if (viewId == R.id.ivChangePwdStatus) {
            if (pwdIsShow) {
                binding.etInputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding.ivChangePwdStatus.setImageResource(R.mipmap.aa_icon_to_view);
            } else {
                binding.etInputPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.ivChangePwdStatus.setImageResource(R.mipmap.aa_icon_close_look_at);
            }
            pwdIsShow = !pwdIsShow;
            binding.etInputPassword.postInvalidate();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}